<?php
/*
** @introduction: 订单管理
** @author: 杨陈鹏
** @email: yangchenpeng@cdlinglu.com
*/

namespace Admin\Controller\Order;


use Common\Controller\Admin\CommonController;

class IndexController extends CommonController
{

    /*
    ** @introduction: 订单列表
    ** @author: 杨陈鹏
    ** @email: yangchenpeng@cdlinglu.com
    */
    public function lists()
    {

        $logic = D('Common/Order','Logic');

        $this->status = I('param.status');

        $this->delete = I('param.delete',1);

        $this->start_time = trim(I('post.start_time'));
        $this->end_time = trim(I('post.end_time'));
        $this->member = trim(I('post.member'));
        $this->number = trim(I('post.number'));

        if($this->status)
            $where['status'] = $this->status;

        if($this->start_time && $this->end_time)
            $where['create_time'] = array(array('gt',strtotime($this->start_time)),array('lt',strtotime($this->end_time)),'and');

        if($this->start_time && !$this->end_time)
            $where['create_time'] = array('gt',strtotime($this->start_time));

        if(!$this->start_time && $this->end_time)
            $where['create_time'] = array('lt',strtotime($this->end_time));

        if($this->member){

            $member_logic = D('Common/Member','Logic');

            $type = getSignType($this->member);



            $where['uid'] = $member_logic::$model->where([$type['type']=>$type[$type['type']]])->getField('uid');

            if(!$where['uid'])
                $where['uid'] = 0;

        }

        if($this->number)
            $where['number'] = $this->number;

        $where['is_delete'] = $this->delete;

        $this->page = I('post.page',1);

        $this->nums = 20;

        $this->order = 'create_time desc,status asc';

        $data = $logic::getData($where,'*', $this->order, $this->page, $this->nums);

        $this->data = $data['data'];


        $this->total = $data['total'];
//        echo '<pre>';
//
//        var_dump($this->data);
//
//        exit;


        $this->display();

    }
    /*
     ** @introduction: 取消订单
     ** @author: 杨陈鹏
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function cancel()
    {

        $id = I('post.id');

        $logic = D('Common/Order','Logic');

        //解冻资金
        $logic::thawCapital($id);

        $this->ajaxReturn($logic::updateStatus($id,6));


    }
    /*
     ** @introduction: 删除订单
     ** @author: 杨陈鹏
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function delete()
    {

        $id = I('post.id');

        $logic = D('Common/Order','Logic');

        $this->ajaxReturn($logic::update(['id' => $id],['is_delete' => 2]));


    }
    /*
     ** @introduction: 修改运费
     ** @author: 杨陈鹏
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function setFreight()
    {

        $id = I('post.id');

        $freight = I('post.freight');

        $freight = floatval($freight) * 100;

        $logic = D('Common/Order','Logic');

        $data['freight'] = $freight;
        $data['description_freight'] = date('Y-m-d H:i:s').'修改运费为&yen;'.$freight/100;

        $this->ajaxReturn($logic::update(['id' => $id],$data));


    }
    /*
     ** @introduction: 修改价格
     ** @author: 杨陈鹏
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function setPrice()
    {

        $id = I('post.id');

        $price = I('post.price');

        $price = floatval($price) * 100;

        $logic = D('Common/Order','Logic');

        $data['order_price_total'] = $price;
        $data['description_price'] = date('Y-m-d H:i:s').'修改价格为&yen;'.$price/100;

        $this->ajaxReturn($logic::update(['id' => $id],$data));


    }
    /*
     ** @introduction: 订单详情
     ** @author: 杨陈鹏
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function detail()
    {


        $id = I('post.id');

        $logic = D('Common/Order','Logic');

        $data = $logic::detail($id);

        $this->data = $data['data'];



        $this->display();

    }


    /*
     ** @introduction: 发货
     ** @author: 杨陈鹏
     ** @email: yangchenpeng@cdlinglu.com
     */
    public function setFh()
    {

        $data = I('post.data');

        $logic = D('Common/Order','Logic');


        if( $data ) {

            $id = $data['id'];
            $mobile = $data['mobile'];
            $number = $data['number'];
            $express_s_number = $data['express_number'];
            $express_s_name = $data['express_s_name'];
            unset($data['id']);
            unset($data['mobile']);
            unset($data['number']);

            $data['send_time'] = NOW_TIME;
            $data['status'] = 3;
            $data['end_time'] = NOW_TIME + (60 * 60 *24 *7);


            $result = $logic::update(['id' => $id],$data);

            if($result['code'] == 200){

                //发货成功,向用户发送短信
                $sms = new \Think\SMS\Mobile();

                $msg = "您的订单已发货,请注意查收";

                $sms_res = $sms->send($mobile,$msg);

                $result['msg'] = $result['msg'].'|'.$sms_res['msg'];

            }


            $this->ajaxReturn($result);

        }

        $this->id = I('post.id');


        $data = $logic::detail($this->id);

        $this->data = $data['data'];

        //获取快递公司
        $express_logic = D('Common/Express','Logic');

        $express = $express_logic::getData(['state' => 2],'id,name','order_by asc',1,10000);

        $this->express = $express['data'];


        //获取发货信息

        $send_address_logic = D('Common/SendAddress','Logic');

        $send_address   =   $send_address_logic::getData();

        $this->send_address = $send_address['data'];

        foreach ( $send_address['data'] as $v ) {

            if ( $v['is_default'] == 1 )
                $this->send_address_default = $v;

        }

        $this->display();

    }

}